package cn.vic.enums;

/**
 * Created by vic on 2017/8/8.
 */
public enum SqlDataBaseEnum {
    MYSQL("com.mysql.jdbc.Driver", "jdbc:mysql://%s:%s/%s", "3306"),
    ORACLE("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@%s:%s:%s", "1521"),
    SQLSERVER2000("oracle.jdbc.driver.OracleDriver", "jdbc:microsoft:sqlserver://%s:%s;databaseName=%s", "1433"),
    SQLSERVER2005("com.microsoft.sqlserver.jdbc.SQLServerDriver", "jdbc:sqlserver://%s:%s;databaseName=%s", "1433"),
    JTDS("net.sourceforge.jtds.jdbc.Driver", "jdbc:jtds:sqlserver://%s:%s/%s;tds=8.0;lastupdatecount=true", "1433");

    private String driverClass;
    private String defaultUrl;
    private String defaultPort;

    SqlDataBaseEnum(String driverClass, String defaultUrl, String defaultPort) {
        this.driverClass = driverClass;
        this.defaultUrl = defaultUrl;
        this.defaultPort = defaultPort;
    }

    public static String hasDatabaseType(String jdbcUrl) {
        jdbcUrl = jdbcUrl.toLowerCase();
        if (jdbcUrl.indexOf("h2") > -1) {
            return "H2";
        } else if (jdbcUrl.indexOf("mysql") > -1) {
            return "MYSQL";
        } else if (jdbcUrl.indexOf("oracle") > -1) {
            return "ORACLE";
        } else if (jdbcUrl.indexOf("microsoft:sqlserver") > -1) {
            return "SQLSERVER2000";
        } else if (jdbcUrl.indexOf("jdbc:sqlserver") > -1) {
            return "SQLSERVER2005";
        } else if (jdbcUrl.indexOf("jtds") > -1) {
            return "JTDS";
        } else if (jdbcUrl.indexOf("postgresql") > -1) {
            return "POSTGRESQL";
        } else if (jdbcUrl.indexOf("sybase") > -1) {
            return "SYBASE";
        } else if (jdbcUrl.indexOf("db2") > -1) {
            return "DB2";
        } else if (jdbcUrl.indexOf("hsqldb") > -1) {
            return "HSQLDB";
        } else if (jdbcUrl.indexOf("derby") > -1) {
            return "DERBY";
        }
        return null;
    }

    public static SqlDataBaseEnum getEnumByJdbcUrl(String jdbcUrl) {
        String databaseType = SqlDataBaseEnum.hasDatabaseType(jdbcUrl);
        return SqlDataBaseEnum.getEnum(databaseType);
    }


    //通过name获取对应的枚举对象
    public static SqlDataBaseEnum getEnum(String dabaName) {
        for (SqlDataBaseEnum examType : SqlDataBaseEnum.values()) {
            if (dabaName.equals(examType.name())) {
                return examType;
            }
        }
        return null;
    }

    public String getDriverClass() {
        return driverClass;
    }

    public void setDriverClass(String driverClass) {
        this.driverClass = driverClass;
    }

    public String getDefaultUrl(String... strings) {
        return String.format(defaultUrl, (Object[]) strings);
    }

    public void setDefaultUrl(String defaultUrl) {
        this.defaultUrl = defaultUrl;
    }

    public String getDefaultPort() {
        return defaultPort;
    }

    public void setDefaultPort(String defaultPort) {
        this.defaultPort = defaultPort;
    }
}
